//
//  JourneyTVController.m
//  Travel assistant
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "JourneyTVController.h"
#import "DetailsTVController.h"
#import "AttractionsVController.h"
#import "WeatherVController.h"
#import "DiaryViewController.h"
#import "HotelTableViewController.h"
#import "DirectoryTVController.h"

@interface JourneyTVController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation JourneyTVController
static NSString *const noteTViewCell = @"NoteTViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self showTabBar];
    /**设置代理*/
    [self setDelegate];
    [self setRegisterCell];
    /**自定义导航栏*/
    [self diyNavigationBar];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:241 / 255.0 alpha:1];
    
    DirectoryTVController *directoryTVC = [[DirectoryTVController alloc] init];
    [self addChildViewController:directoryTVC
     ];

    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    /**获取通知中心的单利对象*/
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    /**添加当前类为一个观察者*/
    [center addObserver:self selector:@selector(changeWeatherLogo) name:@"changeWeatherButton" object:nil];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"行程" style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
}
- (void)viewWillAppear:(BOOL)animated{
    [self showTabBar];
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender{
    NSLog(@"跳转到行程目录界面。。。。。");
    
    
    
    
    
    /**
     点击了左边的按钮 当前View的Y值减小->(x 等比例缩小)
     */
    /**计算减小的Y值(获得当前显示的视图）*/
    UIView *currentView = self.navigationController.view;
    [UIView animateWithDuration:0.5 animations:^{
        self.hidesBottomBarWhenPushed = YES;
        /**设置transform(缩放和平移)*/
        CGFloat navH = [[UIScreen mainScreen] bounds].size.height - 2 * 60;
        CGFloat scale = navH / Screen_H;
        /**设置右边的间距*/
        CGFloat rightMargin = Screen_W *(1 - scale) * 0.5;
        CGFloat translateX = 200 - rightMargin;
        /**设置上边的间距*/
        CGFloat topMargin = Screen_H * (1 - scale) * 0.5;
        CGFloat translateY = topMargin - 60;
        //缩放
        CGAffineTransform scaleForm = CGAffineTransformMakeScale(scale, scale);
        /**平移*/
        CGAffineTransform translateForm = CGAffineTransformTranslate(scaleForm, translateX / scale, translateY / scale);
        currentView.transform = translateForm;
        self.tabBarController.tabBar.hidden = YES;
    } completion:^(BOOL finished) {
        /**添加一个遮盖*/
        UIButton *coverButton = [UIButton buttonWithType:UIButtonTypeCustom];
        coverButton.frame = currentView.bounds;
        coverButton.alpha = 1;
        [currentView addSubview:coverButton];
        [coverButton addTarget:self action:@selector(coverButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
//    DirectoryTVController *directoryTV = [[DirectoryTVController alloc] init];
//    /**push跳转时需要隐藏tabBar*/
//    self.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:directoryTV animated:YES];
//    /**这样back回来的时候，tabBar正常显示*/
//    self.hidesBottomBarWhenPushed = NO;

}
- (void)coverButtonAction:(UIButton *)sender{
    /**取消transform*/
    UIView *currentView = self.navigationController.view;
    [UIView animateWithDuration:0.5 animations:^{
        //恢复transfrom
        currentView.transform = CGAffineTransformIdentity;
        
    }];
    /**移除遮盖*/
    [sender removeFromSuperview];
    sender = nil;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100.0), dispatch_get_main_queue(), ^{
        self.tabBarController.tabBar.hidden = NO;
        NSLog(@"%@",self.superclass);
    });

}



/**设置代理*/
- (void)setDelegate{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
}
/**注册Cell*/
- (void)setRegisterCell{
    [self.tableView registerNib:[UINib nibWithNibName:@"NoteTViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:noteTViewCell];
}
- (void)diyNavigationBar{
    /**KVC*/
    //[self.navigationController setValue:[[DIYNavigationBar alloc] init] forKeyPath:@"navigationBar"];
    
}
- (void)changeWeatherLogo{
    
//    WeatherView *weatherView = [[WeatherView alloc] ];
//    weatherView.cancelButton.backgroundColor = [UIColor cyanColor];
//    [self.view addSubview:weatherView];
    
//     [[[NSBundle mainBundle] loadNibNamed:@"WeatherView" owner:nil options:nil] firstObject];
    NSLog(@"弹出切换天气的View");
    WeatherVController *weatherVC = [[WeatherVController alloc] init];
    weatherVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //weatherVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    
    
//    weatherVC.weatherBlock = ^(NSInteger index){
//        NSLog(@"....................%ld",index);
//#pragma mark ---切换图片------
//    };
    
    
    [self presentViewController:weatherVC animated:YES completion:^{
        NSLog(@"模态到weatherVC。。。。。");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView的代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NoteTViewCell *cell = [tableView dequeueReusableCellWithIdentifier:noteTViewCell forIndexPath:indexPath];
    
    // Configure the cell...
    /**Block回调方便页面跳转   */
    cell.noteTViewCellBlock = ^(NSIndexPath *noteCell_indexPath){
        DetailsTVController *detailsTVC;
        AttractionsVController *attractionsVC;
        DiaryViewController *diaryVC;
        HotelTableViewController *hotelTVC;
        switch (noteCell_indexPath.row) {
                
            case 2:
            case 3:
                detailsTVC = [[DetailsTVController alloc] init];
                detailsTVC.receivedIndex = noteCell_indexPath.row - 2;
                [self hideTabBar];
                [self.navigationController pushViewController:detailsTVC animated:YES];
                break;
            case 4:
                attractionsVC = [[AttractionsVController alloc] init];
                /**push跳转时需要隐藏tabBar*/
                [self hideTabBar];
                [self.navigationController pushViewController:attractionsVC animated:YES];
                /**这样back回来的时候，tabBar正常显示*/
                break;
            case 5:
                hotelTVC = [[HotelTableViewController alloc] init];
                /**push跳转时需要隐藏tabBar*/
                [self hideTabBar];
                [self.navigationController pushViewController:hotelTVC animated:YES];
                /**这样back回来的时候，tabBar正常显示*/
                break;
            case 6:
                diaryVC = [[DiaryViewController alloc] init];
                [self hideTabBar];
                /**push跳转时需要隐藏tabBar*/
                [self.navigationController pushViewController:diaryVC animated:YES];
                /**这样back回来的时候，tabBar正常显示*/
                break;
        }
    };
    return cell;
}
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 270;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    
}
- (void)hideTabBar {
    if (self.tabBarController.tabBar.hidden == YES) {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)showTabBar

{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    
    else
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = NO;
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
